package cn.pengpeng.day02;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * java api 操作HDFS
 * @author root
 *
 */
public class HDFSDemo {
	FileSystem fs = null;
	@Before
	public void init() throws Exception{
		Configuration conf = new Configuration();
		//dfs.blocksize   134217728 = 1024*1024*128    128M    67108864
		//dfs.replication    3
		//conf.set("dfs.blocksize", "134217728");
		//conf.set("dfs.replication", "1");
		fs  = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "root");
	}
	
	/**
	 * 上传文件
	 * @throws URISyntaxException 
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	@Test
	public void testUpLoad() throws IOException, InterruptedException, URISyntaxException{
		//196M   64m   =4
		fs.copyFromLocalFile(new Path("D:\\hello.txt"),new Path("/hello.txt"));
	}
	
	/**
	 * 下载文件
	 * 需要HADOOP_HOME   和 PATH
	 * 还需要解压一个windows编译好的包，复制到hadoop_home下面的bin目录去
	 * 
	 */
	@Test
	public void testDownLoad() throws Exception{
		fs.copyToLocalFile(new Path("/rating.json"), new Path("d:\\rating.json"));
	}
	
	/**
	 * 删除文件
	 * @throws IOException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void testDelete() throws Exception{
		fs.delete(new Path("/rating.json"), true);
	}
	
	/**
	 * 创建目录
	 * @throws IOException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void mkdir() throws IllegalArgumentException, IOException{
		fs.mkdirs(new Path("/mkdir"));
	}
	/**
	 * 改名，移动
	 * @throws IOException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void rename() throws IllegalArgumentException, IOException{
		//移动的时候需要目录存在，不存在的时候不抛异常，但是执行不成功
		fs.rename(new Path("/file2.txt"), new Path("/mkdir/file.txt"));
	}
	
	
	/**
	 * 文件的状态    列出来的都是文件
	 * @throws IllegalArgumentException 
	 * @throws FileNotFoundException 
	 * @throws IOException 
	 */
	@Test
	public void listFiles() throws FileNotFoundException, IllegalArgumentException, IOException{
		//得到有哪些文件的迭代器
		RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
		while(listFiles.hasNext()){
			//单个文件
			LocatedFileStatus fileInfo = listFiles.next();
			System.out.println(fileInfo.getLen()); //得到文件长度
			System.out.println(fileInfo.getBlockSize()); //得到文件快大小
			System.out.println(fileInfo.getPath()); //得到路径
			System.out.println(fileInfo.getAccessTime());  //得到最后修改时间
			System.out.println(fileInfo.getReplication());   //副本数量
			System.out.println("--------------------------------");
			//获取所有文件块    文件块对应的偏移量信息   存放在哪里
			BlockLocation[] blockLocations = fileInfo.getBlockLocations();
			
			for (BlockLocation blockLocation : blockLocations) {
				System.out.println(blockLocation);
			}
			System.out.println("+++++++++++++++");
		}
		
	}
	
	
	/**
	 * 列出文件夹下面的所有文件以及目录
	 * @throws IllegalArgumentException 
	 * @throws FileNotFoundException 
	 * @throws IOException
	 */
	@Test
	public void listStatus() throws FileNotFoundException, IllegalArgumentException, IOException{
		FileStatus[] listStatus = fs.listStatus(new Path("/"));
		for (FileStatus fileStatus : listStatus) {
			if(fileStatus.isDirectory()){
				System.out.println("这是一个文件夹");
			}
			if(fileStatus.isFile()){
				System.out.println("这是一个文件");
			}
			System.out.println(fileStatus.getLen()); //得到文件长度
			System.out.println(fileStatus.getBlockSize()); //得到文件快大小
			System.out.println(fileStatus.getPath()); //得到路径
			System.out.println(fileStatus.getAccessTime());  //得到最后修改时间
			System.out.println(fileStatus.getReplication());   //副本数量
			
			System.out.println("----------------------------------");
		}
	}
	
	
	
	@After
	public void close() throws IOException{
		fs.close();
	}

}
